﻿@inject IStringLocalizer<Geolocations> Localizer
@inject IGeoLocationService GeoLocationService
@implements IAsyncDisposable

<div>
    @if (WatchID == 0)
    {
        <Button Text="@Localizer["GetLocationButtonText"]" OnClick="GetLocation"></Button>
        <Button Text="@Localizer["WatchPositionButtonText"]" OnClick="WatchPosition"></Button>
    }
    else
    {
        <Button Text="@Localizer["ClearWatchPositionButtonText"]" OnClick="ClearWatchPosition"></Button>
    }
    @if (Model != null)
    {
        <div class="form-inline row g-3 mt-3">
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Longitude" ShowLabel="true" DisplayText="@Localizer["Longitude"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Latitude" ShowLabel="true" DisplayText="@Localizer["Latitude"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Accuracy" ShowLabel="true" DisplayText="@Localizer["Accuracy"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Altitude" ShowLabel="true" DisplayText="@Localizer["Altitude"]" />
            </div>
            <div class="col-12 col-sm-4">
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.AltitudeAccuracy" ShowLabel="true" DisplayText="@Localizer["AltitudeAccuracy"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Heading" ShowLabel="true" DisplayText="@Localizer["Heading"]" />
            </div>
            <div class="col-12 col-sm-4">
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.Speed" ShowLabel="true" DisplayText="@Localizer["Speed"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.CurrentDistance" ShowLabel="true" DisplayText="@Localizer["CurrentDistance"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.TotalDistance" ShowLabel="true" DisplayText="@Localizer["TotalDistance"]" />
            </div>
            <div class="col-12 col-sm-4">
                <Display Value="@Model.LastUpdateTime" ShowLabel="true" DisplayText="@Localizer["LastUpdateTime"]" />
            </div>
        </div>
    }
    <ConsoleLogger @ref="Logger" class="mt-3" />
</div>

@code {
    [NotNull]
    private ConsoleLogger? Logger { get; set; }

    private GeolocationPosition? Model { get; set; }

    /// <summary>
    /// 获得/设置 获取持续定位监听器ID
    /// </summary>
    private long WatchID { get; set; }

    private async Task GetLocation()
    {
        Model = await GeoLocationService.GetPositionAsync();
    }

    private async Task WatchPosition()
    {
        try
        {
            WatchID = await GeoLocationService.WatchPositionAsync(p =>
            {
                Model = p;
                StateHasChanged();
                return Task.CompletedTask;
            });
            Logger.Log(WatchID != 0 ? Localizer["WatchPositionResultSuccess"] : Localizer["WatchPositionResultFailed"]);
            Logger.Log($"WatchID : {WatchID}");
        }
        catch (Exception)
        {
            Logger.Log(Localizer["WatchPositionResultFailed"]);
        }
    }

    private async Task ClearWatchPosition()
    {
        if (WatchID != 0)
        {
            var ret = await GeoLocationService.ClearWatchPositionAsync(WatchID);
            if (ret)
            {
                WatchID = 0;
            }
            Logger.Log(ret ? Localizer["ClearWatchPositionResultSuccess"] : Localizer["ClearWatchPositionResultFailed"]);
        }
    }

    /// <summary>
    /// DisposeAsync
    /// </summary>
    /// <param name="disposing"></param>
    protected virtual async ValueTask DisposeAsync(bool disposing)
    {
        if (disposing)
        {
            if (WatchID != 0)
            {
                await GeoLocationService.ClearWatchPositionAsync(WatchID);
            }
        }
    }

    /// <summary>
    /// <inheritdoc/>
    /// </summary>
    public async ValueTask DisposeAsync()
    {
        await DisposeAsync(true);
        GC.SuppressFinalize(this);
    }
}
